package cn.bluarry.weathert.util;

import android.text.TextUtils;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.bluarry.weathert.db.City;
import cn.bluarry.weathert.db.County;
import cn.bluarry.weathert.db.Province;
import cn.bluarry.weathert.gson.Weather;


public class Utility {
    /**
     * 解析和处理服务器返回的省级数据
     */
    public static boolean handleProvinceResponse(String response){
        if(!TextUtils.isEmpty(response)){
            try {
                JSONArray allProvinces=new JSONArray(response);
                for(int i=0;i<allProvinces.length();i++){
                    JSONObject provinceobj=allProvinces.getJSONObject(i);
                    Province p=new Province();
                    p.setProvinceCode(provinceobj.getInt("id"));
                    p.setProvinceName(provinceobj.getString("name"));
                    p.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return false;
    }

    /**
     * 解析市级数据
     * @param response
     * @return
     */
    public static boolean handleCityResponse(String response,int provinceId){
        if(!TextUtils.isEmpty(response)){
            try {
                JSONArray allProvinces=new JSONArray(response);
                for(int i=0;i<allProvinces.length();i++){
                    JSONObject obj=allProvinces.getJSONObject(i);
                    City c=new City();
                    c.setCityCode(obj.getInt("id"));
                    c.setCityName(obj.getString("name"));
                    c.setProvinceId(provinceId);
                    c.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
    /***
     * 解析县级数据
     */
    public static boolean handleContryResponse(String response,int CityId){
        if(!TextUtils.isEmpty(response)){
            try {
                JSONArray allProvinces=new JSONArray(response);
                for(int i=0;i<allProvinces.length();i++){
                    JSONObject obj=allProvinces.getJSONObject(i);
                    County c=new County();
                    c.setCityId(CityId);
                    c.setCoutyName(obj.getString("name"));
                    c.setWeatherId(obj.getString("weather_id"));
                    c.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return false;
    }

    public static Weather handleWeatherResponse(String response){
        try {
            JSONObject jsonObject=new JSONObject(response);
            JSONArray jsonArray=jsonObject.getJSONArray("HeWeather");
            String weatherContent=jsonArray.getJSONObject(0).toString();
            return new Gson().fromJson(weatherContent,Weather.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }



}
